<script setup lang="ts">
import {
  PieChartOutlined, DesktopOutlined, InboxOutlined, MailOutlined, AppstoreOutlined, MenuUnfoldOutlined, MenuFoldOutlined,
} from '@ant-design/icons-vue';
import {
  computed, ref, reactive, watch,
} from 'vue';
import logoImg from '@/assets/logo.png';

const logoImage = computed(() => logoImg);
// 导航菜单相关
const collapsed = ref(false);
const selectedKeys = ['1']; // 当前选中的菜单项 key 数组
let openKeys = reactive(['sub1']); // 当前展开的 SubMenu 菜单项 key 数组
let preOpenKeys = reactive(['sub1']); // 上一次展开的 SubMenu 菜单项 key 数组

watch(
  openKeys,
  (keys, preKeys) => {
    preOpenKeys = preKeys;
  },
);
// 菜单缩起/展开
const toggleCollapsed = () => {
  collapsed.value = !collapsed.value;
  openKeys = collapsed.value ? [] : preOpenKeys;
};
</script>

<template>
  <div class="side-bar-container">
    <div class="logo">
      <img class="logo-img" :src="logoImage" alt="logo">
      <span class="logo-title">ADMIN</span>
    </div>
    <div class="menu-scroll-content">
      <a-menu
        v-model:openKeys="openKeys"
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        theme="dark"
        :inline-collapsed="collapsed"
      >
        <a-menu-item key="1">
          <template #icon>
            <PieChartOutlined />
          </template>
          <span>Option 1</span>
        </a-menu-item>
        <a-menu-item key="2">
          <template #icon>
            <DesktopOutlined />
          </template>
          <span>Option 2</span>
        </a-menu-item>
        <a-menu-item key="3">
          <template #icon>
            <InboxOutlined />
          </template>
          <span>Option 3</span>
        </a-menu-item>
        <a-sub-menu key="sub1">
          <template #icon>
            <MailOutlined />
          </template>
          <template #title>Navigation One</template>
          <a-menu-item key="5">Option 5</a-menu-item>
          <a-menu-item key="6">Option 6</a-menu-item>
          <a-menu-item key="7">Option 7</a-menu-item>
          <a-menu-item key="8">Option 8</a-menu-item>
        </a-sub-menu>
        <a-sub-menu key="sub2">
          <template #icon>
            <AppstoreOutlined />
          </template>
          <template #title>Navigation Two</template>
          <a-menu-item key="9">Option 9</a-menu-item>
          <a-menu-item key="10">Option 10</a-menu-item>
          <a-sub-menu key="sub3" title="Submenu">
            <a-menu-item key="11">Option 11</a-menu-item>
            <a-menu-item key="12">Option 12</a-menu-item>
          </a-sub-menu>
        </a-sub-menu>
        <a-sub-menu key="sub3">
          <template #icon>
            <AppstoreOutlined />
          </template>
          <template #title>Navigation Three</template>
          <a-menu-item key="13">Option 13</a-menu-item>
          <a-menu-item key="14">Option 14</a-menu-item>
          <a-sub-menu key="sub3" title="Submenu">
            <a-menu-item key="15">Option 15</a-menu-item>
            <a-menu-item key="16">Option 16</a-menu-item>
          </a-sub-menu>
        </a-sub-menu>
      </a-menu>
    </div>
    <div class="button-warpper" @click="toggleCollapsed">
      <MenuUnfoldOutlined v-if="collapsed" />
      <MenuFoldOutlined v-else />
    </div>
  </div>
</template>

<style lang="less" scoped>
.side-bar-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .logo {
    flex: none;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    .logo-img {
      display: block;
      width: 32px;
      height: 32px;
      border-radius: 50%;
    }
    .logo-title {
      font-size: 20px;
      color: #fff;
      margin-left: 10px;
      line-height: 1;
    }
  }
  .menu-scroll-content {
    flex: 1;
    overflow: hidden auto;
  }
  .button-warpper {
    flex: none;
    padding: 0 16px;
    height: 40px;
    display: flex;
    align-items: center;
    color: #fff;
    cursor: pointer;
    border-top: #00101e 1px solid;
  }
}
</style>
